home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / m_correlate.pro < prev    next >
Text File  |  1997-07-08  |  5KB  |  127 lines

  1. ;$Id: m_correlate.pro,v 1.6 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1994-1997, Research Systems, Inc.  All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5. ;+
  6. ; NAME:
  7. ;       M_CORRELATE
  8. ;
  9. ; PURPOSE:
  10. ;       This function computes the multiple correlation coefficient of a
  11. ;       dependent variable and two or more independent variables.
  12. ;
  13. ; CATEGORY:
  14. ;       Statistics.
  15. ;
  16. ; CALLING SEQUENCE: 
  17. ;       Result = M_correlate(X, Y)
  18. ;                           
  19. ; INPUTS:
  20. ;       X:    An array of m-columns and n-rows of type integer, float or double
  21. ;             that specifies the independent variable data. The columns of this
  22. ;             two dimensional array correspond to the n-element vectors of 
  23. ;             independent variable data.
  24. ;
  25. ;       Y:    An n-element vector of type integer, float or double that
  26. ;             specifies the dependent variable data.
  27. ;
  28. ; KEYWORD PARAMETERS:
  29. ;    DOUBLE:  If set to a non-zero value, computations are done in
  30. ;             double precision arithmetic.
  31. ;
  32. ; EXAMPLE:
  33. ;       Define the independent (X) and dependent (Y) data.
  34. ;         X = [[0.477121, 2.0, 13.0], $
  35. ;              [0.477121, 5.0,  6.0], $
  36. ;              [0.301030, 5.0,  9.0], $
  37. ;              [0.000000, 7.0,  5.5], $
  38. ;              [0.602060, 3.0,  7.0], $
  39. ;              [0.698970, 2.0,  9.5], $
  40. ;              [0.301030, 2.0, 17.0], $
  41. ;              [0.477121, 5.0, 12.5], $
  42. ;              [0.698970, 2.0, 13.5], $
  43. ;              [0.000000, 3.0, 12.5], $
  44. ;              [0.602060, 4.0, 13.0], $
  45. ;              [0.301030, 6.0,  7.5], $
  46. ;              [0.301030, 2.0,  7.5], $
  47. ;              [0.698970, 3.0, 12.0], $
  48. ;              [0.000000, 4.0, 14.0], $
  49. ;              [0.698970, 6.0, 11.5], $
  50. ;              [0.301030, 2.0, 15.0], $
  51. ;              [0.602060, 6.0,  8.5], $
  52. ;              [0.477121, 7.0, 14.5], $
  53. ;              [0.000000, 5.0, 9.5]]
  54. ;
  55. ;          Y = [97.682, 98.424, 101.435, 102.266,  97.067,  97.397, 99.481, $
  56. ;               99.613, 96.901, 100.152,  98.797, 100.796,  98.750, 97.991, $
  57. ;              100.007, 98.615, 100.225,  98.388,  98.937, 100.617]
  58. ;
  59. ;       Compute the multiple correlation of Y on the first column of X.
  60. ;       The result should be 0.798816
  61. ;         result = m_correlate(X(0,*), Y)
  62. ;
  63. ;       Compute the multiple correlation of Y on the first two columns of X.
  64. ;       The result should be 0.875872
  65. ;         result = m_correlate(X(0:1,*), Y)
  66. ;
  67. ;       Compute the multiple correlation of Y on all columns of X.
  68. ;       The result should be 0.877197
  69. ;         result = m_correlate(X, Y)
  70. ;         
  71. ; PROCEDURE:
  72. ;       M_CORRELATE uses relationships based upon partial correlation to
  73. ;       compute the multiple correlation coefficient of linear models with
  74. ;       two or more independent variables: y(x0, x1), y(x0, x1, ... , xn-1).   
  75. ;
  76. ; REFERENCE:
  77. ;       APPLIED STATISTICS (third edition)
  78. ;       J. Neter, W. Wasserman, G.A. Whitmore
  79. ;       ISBN 0-205-10328-6
  80. ;
  81. ; MODIFICATION HISTORY:
  82. ;       Written by:   GGS, RSI, July 1994
  83. ;       Modified by:  GGS, RSI, August 1996
  84. ;                     Added DOUBLE keyword.
  85. ;                     Modified keyword checking and use of double precision.
  86. ;-
  87.  
  88. FUNCTION  M_Correlate, X, Y, Double = Double
  89.  
  90.   ON_ERROR, 2  ;Return to caller if an error occurs.
  91.  
  92.   Sx = SIZE(x)  &  Sy = SIZE(y)
  93.  
  94.   if Sx[0] ne 2 then MESSAGE, $
  95.     "X parameter must be a two-dimensional array."
  96.  
  97.   ;Check row dimension of X.
  98.   if Sx[Sx[0]] ne Sy[Sy[0]+2] then MESSAGE, $
  99.     "Incompatible arrays."
  100.  
  101.   if N_ELEMENTS(Double) eq 0 then $
  102.     Double = (Sx[Sx[0]+1] eq 5) or (Sy[Sy[0]+1] eq 5)
  103.  
  104.   nVars = Sx[1] ;Number of independent variables (columns of X).
  105.  
  106.   ;Coefficient of Multiple Determination:
  107.   comd = (1 - CORRELATE(X[0,*], Y, Double = Double)^2)
  108.  
  109.   for k = 0, nVars-2 do $
  110.     ;Compute the Coefficient of Multiple Determination using a product
  111.     ;of partial correlations.
  112.     comd = comd * (1 - $
  113.            P_CORRELATE(TRANSPOSE(X[k+1,*]), Y, X[0:k,*], Double = Double)^2)
  114.  
  115.     ;For example:
  116.     ;The Coefficient of Multiple Determination for a model with 5 
  117.     ;independent parameters y(x0, x1, x2, x3, x4) is computed as:
  118.     ;comd = (1 - correlate(x(0,*), y)^2) * $
  119.     ;       (1 - p_correlate(transpose(x(1,*)), y, x(0,*))^2)   * $
  120.     ;       (1 - p_correlate(transpose(x(2,*)), y, x(0:1,*))^2) * $
  121.     ;       (1 - p_correlate(transpose(x(3,*)), y, x(0:2,*))^2) * $
  122.     ;       (1 - p_correlate(transpose(x(4,*)), y, x(0:3,*))^2)  
  123.  
  124.   RETURN, SQRT(1 - comd)
  125.  
  126. END
  127.